package com.example.gongzhong1.utils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiayuxuanmin
 * @Date 2019/4/28
 * @Description
 * 微信菜单创建Controller
 */
@RestController
public class CreateController {

    @RequestMapping("/createMenu")
    public String createMenu(){
        AccessToken accessToken = WeixinUtil.getAccessToken(WeixinUtil.APPID, WeixinUtil.APPSECRET);
        //if包裹快捷键:accessToken.nn,或者:accessToken != null.if
        if (accessToken == null) {
            return "token 获取失败";
        }else{
            int menu = WeixinUtil.createMenu(init(), accessToken.getToken());
            //快捷键:menu==0.if
            if (menu==0) {
                return "success";
            }else{
                return "fail";
            }
        }
    }

    /**
     *  @Description 初始化菜单
     *  @Param
     *  @Return
     *  @Author xiayuxuanmin
     *  @Date 2019/4/28
     */
    private Menu init() {
        Menu menu = new Menu();

        List<Button> buttons = new ArrayList<>();
        ViewButton viewButton = new ViewButton();
        viewButton.setName("goole1一下");
        viewButton.setUrl("http://www.baidu.com");
//        viewButton.setType("View");

        buttons.add(viewButton);

        ClickButton clickButton = new ClickButton();
        clickButton.setName("购买哦");
        clickButton.setKey("goumai");

        buttons.add(clickButton);

        CommonButton commonButton = new CommonButton();
        commonButton.setName("发牌器");
        List<Button> sub_button = new ArrayList<>();
        commonButton.setSub_button(sub_button);

        ClickButton clickButton1 = new ClickButton();
        clickButton1.setName("创建房间");
        clickButton1.setKey("create");
        sub_button.add(clickButton1);

        ClickButton clickButton2 = new ClickButton();
        clickButton2.setName("进入房间");
        clickButton2.setKey("join");
        sub_button.add(clickButton2);

        ClickButton clickButton3 = new ClickButton();
        clickButton3.setName("重开一局");
        clickButton3.setKey("create");
        sub_button.add(clickButton3);

        ClickButton clickButton4 = new ClickButton();
        clickButton4.setName("刷新");
        clickButton4.setKey("refresh");
        sub_button.add(clickButton4);

        ClickButton clickButton5 = new ClickButton();
        clickButton5.setName("重开一局");
        clickButton5.setKey("recreate");
        sub_button.add(clickButton5);

        buttons.add(commonButton);
        menu.setButton(buttons);
        return menu;
    }
}
